home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-01-28 | 18.9 KB | 826 lines | [TEXT/MPS ] |
- /*
- File: ObjModelTokens.cp
-
- Contains: Token classes implementation
-
- Developed by:
-
- Paul G Smith (commstalk hq & Full Moon Software, Inc)
-
- you can leave messages at (UK): 0727 844232; (US): 408 253 7199
- BUT I prefer to be contacted by e-mail
- AppleLink: SMITH.PG
- Internet: SMITH.PG@applelink.apple.com
-
- "SimpliFace" Sample code to accompany develop article
- on techniques for embedding scripts in applications.
-
-
- Apple Event Object Model tokens for SimpliFace
-
- */
-
-
- #ifndef __MENUS__
- #include <Menus.h>
- #endif
- #ifndef __EVENTS__
- #include <Events.h>
- #endif
- #ifndef __WINDOWS__
- #include <Windows.h>
- #endif
- #ifndef __DIALOGS__
- #include <Dialogs.h>
- #endif
- #ifndef __QUICKDRAW__
- #include <Quickdraw.h>
- #endif
- #ifndef __MEMORY__
- #include <Memory.h>
- #endif
- #ifndef __FILES__
- #include <Files.h>
- #endif
- #ifndef __STANDARDFILE__
- #include <StandardFile.h>
- #endif
- #ifndef __SYSEQU__
- #include <SysEqu.h>
- #endif
- #ifndef __PLSTRINGFUNCS__
- #include <PLStringFuncs.h>
- #endif
-
- #ifndef __AERegistry__
- #include <AERegistry.h>
- #endif
- #ifndef __ASREGISTRY__
- #include <ASRegistry.h>
- #endif
- #ifndef __APPLEEVENTS__
- #include <AppleEvents.h>
- #endif
- #ifndef __AEOBJECTS__
- #include <AEObjects.h>
- #endif
-
- #ifndef __AEOBJECTPACKING__
- #include <AEPackObject.h>
- #endif
-
- #ifndef __AEOMTOKENS__
- #include "ObjModelTokens.h"
- #endif
-
- #ifndef __AEOMEVENTS__
- #include "ObjModelEvents.h"
- #endif
-
- #ifndef __SCRIPTUTILS__
- #include "ScriptUtils.h"
- #endif
-
-
-
- #pragma segment ObjectAccessors
-
-
-
- // ------------
-
-
- // ------------
-
- #pragma trace off
-
- // utility routines
-
- objModelTokenPtr ObjModelTokenFromDesc(AEDesc *theDesc)
- {
- objModelTokenPtr theToken = nil;
- Size actSize;
-
- if (theDesc->descriptorType == typeObjModelToken)
- GetRawDataFromDescriptor(theDesc, (Ptr)&theToken, sizeof(theToken), &actSize);
-
- return theToken;
- }
-
- OSErr DescFromObjModelToken(const objModelTokenPtr theToken, AEDesc *theDesc)
- {
- return AECreateDesc(typeObjModelToken, (Ptr)&theToken, sizeof(theToken), theDesc);
- }
-
-
- // -------------------------------------------------------
-
- #pragma segment ObjectAccessors
-
- #pragma trace off
-
-
- TObjModelToken::TObjModelToken(void)
- {
- fTokenClass = typeNull;
- fIsProperty = false;
- fPropertyID = typeNull;
- fTheObject = NULL;
- }
-
-
- TObjModelToken::TObjModelToken(DescType theTokenClass,
- TScriptableObject* theObj)
- {
- fTokenClass = theTokenClass;
- fIsProperty = false;
- fPropertyID = typeNull;
- fTheObject = theObj;
- }
-
-
- TObjModelToken::TObjModelToken(const TObjModelToken& oldObj)
- {
- fTokenClass = oldObj.fTokenClass;
- fIsProperty = oldObj.fIsProperty;
- fPropertyID = oldObj.fPropertyID;
- fTheObject = oldObj.fTheObject;
- }
-
-
- TObjModelToken& TObjModelToken::operator=(const TObjModelToken& oldObj)
- {
- if (this != &oldObj)
- {
- fTokenClass = oldObj.fTokenClass;
- fIsProperty = oldObj.fIsProperty;
- fPropertyID = oldObj.fPropertyID;
- fTheObject = oldObj.fTheObject;
- }
- return *this;
- }
-
-
- TObjModelToken::~TObjModelToken(void)
- {
- // nothing to do!
- }
-
-
- TObjModelToken* TObjModelToken::MakeClone(void)
- {
- return new TObjModelToken(*this);
- }
-
-
- // support for object accessors
-
-
- TObjModelToken* TObjModelToken::MakeNewToken (DescType theTokenClass,
- TScriptableObject* theObj)
- {
- TObjModelToken *resultToken = new TObjModelToken(theTokenClass, theObj);
-
- return resultToken;
- }
-
-
- OSErr TObjModelToken::ResolveElement(DescType desiredClass,
- DescType keyForm,
- AEDesc *keyData,
- TObjModelToken **theResultToken)
- {
- OSErr err = errAEEventNotHandled;
- TScriptableObject *theResultObj = NULL;
-
- // this function has been called because no derived class knows
- // how to resolve the specified token
-
- // start by testing keyForm;
-
- if (!fTheObject) // can't do it if no object referenced!
- return errAEEventNotHandled;
- else if (keyForm == formName)
- {
- CStr255 nameStr = "";
-
- err = GetPStringFromDescriptor(keyData, (char *)&nameStr);
-
- if (!err)
- err = fTheObject->ResolveElementByName(desiredClass, nameStr,
- &theResultObj);
- }
- else if (keyForm == formAbsolutePosition)
- {
- short index;
-
- err = GetIntegerFromDescriptor(keyData, &index);
- if (index < 0)
- {
- long numElems;
-
- if (fTheObject->CountElements(desiredClass, &numElems) == 0)
- index = numElems + index + 1;
- }
-
- if (!err)
- err = fTheObject->ResolveElementByIndex(desiredClass, index,
- &theResultObj);
- }
- else
- err = errAEBadKeyForm;
-
- if (!err)
- {
- *theResultToken = MakeNewToken(desiredClass, theResultObj);
- }
- return err;
- }
-
- OSErr TObjModelToken::ResolveProperty(DescType desiredClass,
- DescType keyForm,
- AEDesc *keyData,
- TObjModelToken **theResultToken)
- {
- OSErr err = errAEEventNotHandled;
- DescType theProperty;
- Size actualSize;
-
- if (keyForm == formPropertyID)
- {
- GetRawDataFromDescriptor(keyData, (Ptr)&theProperty,
- sizeof(theProperty), &actualSize);
-
- fIsProperty = true;
- fPropertyID = theProperty;
-
- // make clone of this token and return reference to
- // it in theResultToken (requires use of copy constructors)
-
- *theResultToken = this->MakeClone();
-
- if (*theResultToken)
- err = noErr;
- }
-
- return err;
- }
-
-
- OSErr TObjModelToken::CallDispatchAppleEvent(AppleEvent *theEvent,
- AppleEvent *theReply,
- AEEventClass theEvtClass,
- AEEventID theEvtID)
- {
- return this->DispatchAppleEvent(theEvent, theReply, theEvtClass, theEvtID);
- }
-
-
- OSErr TObjModelToken::DispatchAppleEvent(AppleEvent *theEvent,
- AppleEvent *theReply,
- AEEventClass theEvtClass,
- AEEventID theEvtID)
- {
- OSErr err = errAEEventNotHandled;
-
- if (!fTheObject) // can't do it if no object referenced!
- return err;
-
- if (theEvtClass == kASAppleScriptSuite && theEvtID == kASSubroutineEvent)
- {
- //Trace("Received AS subroutine event (not handled)\n");
- }
- else if (theEvtClass == kAEMiscStandards && theEvtID == kAEDoScript)
- {
- //Trace("Received Do Script event (not handled)\n");
- }
- else if (theEvtClass == kAECoreSuite || theEvtClass == kCoreEventClass)
- {
- switch (theEvtID)
- {
- case kAECountElements:
- err = this->AECountElems(theEvent, theReply);
- break;
- case kAECreateElement:
- err = this->AECreateElem(theEvent, theReply);
- break;
- case kAEDelete:
- err = this->AEDeleteElem(theEvent, theReply);
- break;
- case kAEClose:
- err = this->AECloseObject(theEvent, theReply);
- break;
- case kAEGetData:
- err = this->AEGetObjectData(theEvent, theReply);
- break;
- case kAEOpen:
- err = this->AEOpenObject(theEvent, theReply);
- break;
- case kAESetData:
- err = this->AESetObjectData(theEvent, theReply);
- break;
- }
- }
- return err;
- }
-
-
- // hooks into Apple Event handling, only override for special-purposes;
- // return noErr if Apple Event was handled within the hook function, or
- // errAEEventNotHandled if it was not handled within the hook function
-
- OSErr TObjModelToken::AECountElems (AppleEvent *theEvent,
- AppleEvent *theReply)
- {
- OSErr err = errAEEventNotHandled;
- AEDesc resultDesc;
- Size theSize;
- DescType theType, desiredClass;
- long result;
-
- InitAEDescs(&resultDesc, kEndOfList);
-
- err = AEGetParamPtr(theEvent, keyAEObjectClass, typeType, &theType,
- (Ptr)&desiredClass, sizeof(DescType), &theSize);
-
- if (!err)
- err = fTheObject->CountElements(desiredClass, &result);
-
- if (!err)
- err = AECreateDesc(typeLongInteger, &result, sizeof(long), &resultDesc);
-
- if (!err && theReply->descriptorType != typeNull)
- err = AEPutParamDesc(theReply, keyDirectObject, &resultDesc);
-
- DisposeAEDescs(&resultDesc, kEndOfList);
-
- return err;
- }
-
- OSErr TObjModelToken::AECreateElem (AppleEvent *theEvent,
- AppleEvent *theReply)
- {
- // get insertionloc
- // resolve insertion point
- // get desired object class
- // ask object at insertion point to create new object
- // get new object's data (optional param)
- // save data
- // get new object's properties record (optional param)
- // save properties
- // ask new object to create it's own object specifier
- // return object specifier as result
- OSErr err = errAEEventNotHandled;
- AEDesc resultDesc, dataObject;
- AEDesc insLocParam, insLocObject, theTokenDesc;
- AERecord insLocRec, dataPropParam;
- TObjModelToken *theToken = NULL;
- Size theSize;
- DescType theType, position;
- DescType theClass;
-
- InitAEDescs(&insLocParam, &insLocRec, &insLocObject, &theTokenDesc,
- &resultDesc, &dataObject, &dataPropParam, kEndOfList);
-
- err = AEGetParamPtr(theEvent, keyAEObjectClass, typeType, &theType,
- (Ptr)&theClass, sizeof(DescType), &theSize);
- if (!err)
- err = AEGetParamDesc(theEvent, keyAEInsertHere, typeInsertionLoc, &insLocParam);
- if (err)
- {
- TObjModelToken *aApplicationToken = NULL;
-
- position = kAEEnd;
- err = MakeAppToken(&aApplicationToken);
- if (!err)
- {
- theToken = aApplicationToken;
- DescFromObjModelToken(theToken, &theTokenDesc);
- }
- }
- else
- {
- // coerce the insertion loc record to an AE record.
- err = AECoerceDesc(&insLocParam, typeAERecord, &insLocRec);
- if (!err)
- err = AEGetKeyPtr(&insLocRec, keyAEPosition, typeEnumeration, &theType,
- (Ptr)&position, sizeof(DescType), &theSize);
- if (!err) // Get the object reference
- err = AEGetKeyDesc(&insLocRec, keyAEObject,
- typeWildCard, &insLocObject);
-
- if (!err)
- {
- err = AEResolve(&insLocObject, kAEIDoMinimum, &theTokenDesc);
-
- if (!err)
- theToken = ObjModelTokenFromDesc(&theTokenDesc);
- }
- }
-
- if (theToken)
- {
- TScriptableObject *theNewObj = NULL;
- TObjModelToken *theNewToken = NULL;
- TScriptableObject *containerObj = NULL;
- AERecord *dataPropPtr = NULL;
-
- err = theToken->GetTokenObj()->ResolveContainer(&containerObj);
-
- if (position == kAEReplace &&
- (theToken->GetTokenClass() == cApplication
- || theToken->GetTokenClass() == cDocument))
- err = errAEEventNotHandled;
- else
- {
- err = AEGetParamDesc(theEvent, keyAEData, typeWildCard, &dataObject);
- err = AEGetParamDesc(theEvent, keyAEPropData, typeAERecord,
- &dataPropParam);
- if (!err)
- dataPropPtr = &dataPropParam;
- // ignore errors if above calls fail
- err = theToken->GetTokenObj()->CreateNewElement(theClass, position,
- &dataObject, dataPropPtr,
- containerObj, &theNewObj);
- if (!err)
- theNewToken = MakeNewToken(theClass, theNewObj);
-
- if (!err && theNewToken)
- theNewToken->GetTokenObj()->GetObjectSpecifier(&resultDesc);
- if (theNewToken)
- delete theNewToken;
- }
- }
-
- if (!err && (resultDesc.descriptorType != typeNull)
- && (theReply->descriptorType != typeNull))
- err = AEPutParamDesc(theReply, keyDirectObject, &resultDesc);
-
- AEDisposeToken(&theTokenDesc); // DO NOT CALL until done with 'theToken'
- DisposeAEDescs(&insLocParam, &insLocRec, &insLocObject,
- &resultDesc, &dataObject, &dataPropParam, kEndOfList);
- return err;
- }
-
- OSErr TObjModelToken::AEDeleteElem (AppleEvent *theEvent,
- AppleEvent *theReply)
- {
- OSErr err = fTheObject->DeleteObject();
-
- return err;
- }
-
- OSErr TObjModelToken::AECloseObject (AppleEvent *theEvent,
- AppleEvent *theReply)
- {
- return fTheObject->CloseObject();
- }
-
- OSErr TObjModelToken::AEGetObjectData (AppleEvent *theEvent,
- AppleEvent *theReply)
- {
- OSErr err = errAEEventNotHandled;
- AEDesc resultDesc;
-
- resultDesc.dataHandle = NULL;
-
- if (fIsProperty)
- {
- DescType wantType, actualType;
- Size actualSize;
-
- err = AEGetParamPtr(theEvent, keyAERequestedType, typeType, &actualType,
- (Ptr)&wantType, sizeof(DescType), &actualSize);
- if (err)
- wantType = typeWildCard;
-
- err = fTheObject->GetProperty(fPropertyID, wantType, &resultDesc);
- }
- else
- {
- err = fTheObject->GetData(&resultDesc);
- if (err == errAEEventNotHandled)
- err = AEGetParamDesc(theEvent, keyDirectObject,
- typeObjectSpecifier, &resultDesc);
- }
-
- if (!err && theReply->descriptorType != typeNull)
- err = AEPutParamDesc(theReply, keyDirectObject, &resultDesc);
-
- AEDisposeDesc(&resultDesc);
-
- return err;
- }
-
- OSErr TObjModelToken::AEOpenObject (AppleEvent *theEvent,
- AppleEvent *theReply)
- {
- return fTheObject->OpenObject();
- }
-
- OSErr TObjModelToken::AESetObjectData (AppleEvent *theEvent,
- AppleEvent *theReply)
- {
- OSErr err = errAEEventNotHandled;
- AEDesc dataDesc;
-
- dataDesc.dataHandle = NULL;
-
- err = AEGetParamDesc(theEvent, keyAEData, typeWildCard, &dataDesc);
-
- if (fIsProperty)
- err = fTheObject->SetProperty(fPropertyID, &dataDesc);
- else
- err = fTheObject->SetData(&dataDesc);
-
- AEDisposeDesc(&dataDesc);
-
- return err;
- }
-
-
- // -------------------------------------------------------
-
-
-
- #pragma segment Main
-
- // object accessors
-
-
- OSErr MakeAppToken(TObjModelToken** theApplicationToken)
- {
- OSErr err = errAEEventNotHandled;
-
- *theApplicationToken = new TObjModelToken(cApplication, gSimpliFace);
-
- if (theApplicationToken)
- err = 0;
- else
- err = -108; // out of memory
-
- return err;
- }
-
-
- pascal OSErr PropertyFromNullAccessor (DescType desiredClass,
- AEDesc *containerToken,
- DescType containerClass,
- DescType keyForm,
- AEDesc *keyData,
- AEDesc *theToken,
- long theRefCon)
- {
- OSErr err = errAEEventNotHandled;
- objModelTokenPtr theResolvedToken = NULL;
- TObjModelToken *aApplicationToken = NULL;
-
- if ((desiredClass != cProperty) || (containerClass != typeNull))
- return(errAEWrongDataType);
-
- // we assume property being requested is an application property,
- // and create a temporary application token
- err = MakeAppToken(&aApplicationToken);
- if (!err)
- {
- err = DescFromObjModelToken((const objModelTokenPtr)aApplicationToken, theToken);
-
- if (!err && theToken)
- { // ask the temporary application token to resolve the property
- // (this step creates a new token)
- err = aApplicationToken->ResolveProperty(desiredClass, keyForm, keyData, &theResolvedToken);
- if (!err && theResolvedToken)
- {
- err = DescFromObjModelToken((const objModelTokenPtr)theResolvedToken, theToken);
- }
- }
-
- if (aApplicationToken)
- delete aApplicationToken; // dispose of the temporary application token
- }
- return err;
- }
-
-
- pascal OSErr AppTokenFromNullAccessor (DescType desiredClass,
- AEDesc *containerToken,
- DescType containerClass,
- DescType keyForm,
- AEDesc *keyData,
- AEDesc *theToken,
- long theRefCon)
- {
- OSErr err = errAEEventNotHandled;
- TObjModelToken *aApplicationToken = NULL;
-
- if ((desiredClass != cApplication) || (containerClass != typeNull))
- return(errAEWrongDataType);
-
- err = MakeAppToken(&aApplicationToken);
-
- if (!err)
- err = DescFromObjModelToken((const objModelTokenPtr)aApplicationToken,
- theToken);
-
- return(err);
- }
-
-
- pascal OSErr StdObjectFromNullAccessor (DescType desiredClass,
- AEDesc *containerToken,
- DescType containerClass,
- DescType keyForm,
- AEDesc *keyData,
- AEDesc *theToken,
- long theRefCon)
- {
- OSErr err = errAEEventNotHandled;
- TObjModelToken *aApplicationToken = NULL;
- objModelTokenPtr theResolvedToken = NULL;
-
- if (containerClass != typeNull)
- return(errAEWrongDataType);
-
- err = MakeAppToken(&aApplicationToken);
-
- if (!err)
- {
- err = aApplicationToken->ResolveElement(desiredClass, keyForm,
- keyData, &theResolvedToken);
- if (!err && theResolvedToken)
- {
- err = DescFromObjModelToken((const objModelTokenPtr)theResolvedToken,
- theToken);
- }
- delete aApplicationToken;
- }
-
- return(err);
- }
-
-
-
-
- pascal OSErr StdObjectAccessor (DescType desiredClass,
- AEDesc *containerToken,
- DescType containerClass,
- DescType keyForm,
- AEDesc *keyData,
- AEDesc *theToken,
- long theRefCon)
- {
- OSErr err = errAEEventNotHandled;
- objModelTokenPtr theContToken = ObjModelTokenFromDesc(containerToken);
- objModelTokenPtr theResolvedToken = nil;
-
- if (theToken)
- {
- err = theContToken->ResolveElement(desiredClass, keyForm, keyData, &theResolvedToken);
- if ((err == 0) && theResolvedToken)
- {
- err = DescFromObjModelToken((const objModelTokenPtr)theResolvedToken, theToken);
- }
-
- }
-
- return err;
- }
-
-
- pascal OSErr StdPropertyAccessor(DescType desiredClass,
- AEDesc *containerToken,
- DescType containerClass,
- DescType keyForm,
- AEDesc *keyData,
- AEDesc *theToken,
- long theRefCon)
- {
- OSErr err = errAEEventNotHandled;
- objModelTokenPtr theContToken = ObjModelTokenFromDesc(containerToken);
- objModelTokenPtr theResolvedToken = nil;
-
- if (theContToken && theToken)
- {
- err = theContToken->ResolveProperty(desiredClass, keyForm, keyData, &theResolvedToken);
- if ((err == 0) && theResolvedToken)
- {
- err = DescFromObjModelToken((const objModelTokenPtr)theResolvedToken, theToken);
- }
-
- }
-
- return err;
- }
-
-
- // object support callbacks
-
- pascal OSErr StdCountProc (DescType desiredClass,
- DescType containerClass,
- AEDesc *containerToken,
- long *result)
- {
- OSErr err = errAEEventNotHandled;
- objModelTokenPtr theToken = ObjModelTokenFromDesc(containerToken);
-
- if (theToken)
- err = theToken->GetTokenObj()->CountElements(desiredClass, result);
-
- return err;
- }
-
-
- pascal OSErr StdCompareProc (DescType comparisonOperator,
- AEDesc object,
- DescType objOrDescToCompare,
- Boolean& result)
- {
- OSErr err = errAEEventNotHandled;
-
-
- return err;
- }
-
-
- pascal OSErr StdDisposeToken (AEDesc *unneededToken)
- {
- OSErr err = errAEEventNotHandled;
-
- if (unneededToken)
- {
- objModelTokenPtr theToken = ObjModelTokenFromDesc(unneededToken);
- if (theToken)
- {
- delete theToken;
- err = AEDisposeDesc(unneededToken);
- unneededToken->dataHandle = NULL; // in case OSL deletes us twice
- }
- }
- return err;
- }
-
-
-
- // -------setup
-
-
-
- OSErr InstallAccessors(void);
-
- OSErr DeInstallAccessors(void);
-
-
- // #pragma segment ObjectInit
-
- // set up object model handlers
- OSErr SFinitAEobjects(void)
- {
- OSErr err = 0;
-
- err = InstallAccessors();
-
- return err;
- }
-
- // detach object model handlers & shut down
- OSErr SFendAEobjects(void)
- {
- OSErr err = 0;
-
- err = DeInstallAccessors();
-
- return err;
- }
-
-
- // installation
-
- OSErr InstallAccessors(void)
- {
- OSErr err = 0;
-
- if (!err) err = AEInstallObjectAccessor(cApplication, typeNull, (accessorProcPtr)&AppTokenFromNullAccessor, (long)cApplication, false);
- if (!err) err = AEInstallObjectAccessor(typeWildCard, typeNull, (accessorProcPtr)&StdObjectFromNullAccessor, 0, false);
- if (!err) err = AEInstallObjectAccessor(typeWildCard, typeObjModelToken, (accessorProcPtr)&StdObjectAccessor, 0, false);
-
- if (!err) err = AEInstallObjectAccessor(cProperty, typeNull, (accessorProcPtr)&PropertyFromNullAccessor, (long)cProperty, false);
- if (!err) err = AEInstallObjectAccessor(cProperty, typeObjModelToken, (accessorProcPtr)&StdPropertyAccessor, (long)cProperty, false);
-
- if (!err) err = AESetObjectCallbacks( (compareProcPtr)&StdCompareProc,
- (countProcPtr)&StdCountProc,
- (disposeTokenProcPtr)&StdDisposeToken,
- nil, nil, nil, nil);
-
- return err;
- }
-
- OSErr DeInstallAccessors(void)
- {
- OSErr err = 0;
-
-
- return err;
- }
-
-
-